/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.hanfu.domain.Package;
import top.continew.admin.hanfu.domain.bo.PackageBo;
import top.continew.admin.hanfu.domain.dto.PackageDto;
import top.continew.admin.hanfu.domain.query.PackageQuery;
import top.continew.admin.hanfu.domain.vo.PackageVo;
import top.continew.admin.hanfu.domain.vo.wx.PackageWxVo;
import top.continew.admin.hanfu.service.impl.PackageServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-25
 */

@Slf4j
@RestController
@RequestMapping("/package")
@RequiredArgsConstructor
public class PackageController {

    private final PackageServiceImpl packageService;

    /**
     * 根据条件查询套餐列表
     */
    @PostMapping("/condition")
    public R PackageList(@RequestBody PackageQuery packageQuery) {
        log.info("packageQuery:{}", packageQuery);
        Page<PackageVo> packageListByCondition = packageService.getPackageListByCondition(packageQuery
            .getPageNum(), packageQuery.getPageSize(), packageQuery.getName(), packageQuery.getTypeName(), packageQuery
                .getLable(), packageQuery.getStatus());
        packageListByCondition.getRecords().forEach(i -> {
            i.setUpdateTime(DateTime.of(Long.parseLong(i.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });
        return R.success(packageListByCondition);
    }

    /**
     * 查询套餐 By id
     */
    @GetMapping("/{id}")
    public R getPackageById(@PathVariable("id") Integer id) {

        Package byId = packageService.getById(id);
        return R.success(BeanUtil.copyProperties(byId, PackageDto.class));
    }

    /**
     * 添加套餐接口
     */

    @PostMapping("")
    public R addPackage(@RequestParam("img") List<MultipartFile> img,
                        @RequestParam("name") String packageName,
                        @RequestParam("TypeId") Integer packageTypeId,
                        @RequestParam("price") Double packagePrice,
                        @RequestParam("lable") String packageLable,
                        @RequestParam("service") String service,
                        @RequestParam("goods") String packageGoods,
                        @RequestParam("trip") String packageTrip,
                        @RequestParam("description") String packageDescription,
                        @RequestParam("updateBy") String updateBy,
                        @RequestParam("status") Integer status) {

        PackageBo packageBo = new PackageBo();
        packageBo.setFile(img);
        packageBo.setPackageName(packageName);
        packageBo.setPackageTypeId(packageTypeId);
        packageBo.setPackagePrice(packagePrice);
        packageBo.setPackageLable(packageLable);
        packageBo.setPackageService(service);
        packageBo.setPackageGoods(packageGoods);
        packageBo.setPackageTrip(packageTrip);
        packageBo.setPackageDescription(packageDescription);
        packageBo.setUpdateBy(updateBy);
        packageBo.setStatus(status);
        packageBo.setUpdateTime(String.valueOf(System.currentTimeMillis()));

        int i = packageService.addPackage(packageBo);

        if (i != 1) {
            return R.fail("添加失败");
        }

        return R.success();
    }

    /**
     * 修改接口
     */
    @PutMapping("")
    public R updatePackageService(@RequestBody PackageDto packageDto) {

        int i = packageService.updatePackage(packageDto);
        if (i != 1) {
            return R.fail("修改失败");
        }
        return R.success();
    }

    /**
     * 修改图片
     */
    @PutMapping("/img")
    public R updatePackageImg(@RequestParam("img") List<MultipartFile> img,
                              @RequestParam("id") Integer id,
                              @RequestParam("user") String user,
                              @RequestParam("ossIds") String newOssId) {

        PackageBo packageBo = new PackageBo();

        if (img.get(0).getSize() != 0) {
            packageBo.setFile(img);
        }

        packageBo.setId(id);
        packageBo.setOssIds(newOssId);
        packageBo.setUpdateBy(user);

        int i = packageService.updatePackageImg(packageBo);
        if (i != 1) {
            return R.fail("修改失败");
        }
        return R.success();
    }

    /**
     * 删除接口
     */

    @DeleteMapping("")
    public R deletePackageService(@RequestBody ArrayList<Integer> ids) {

        int i = packageService.deletePackage(ids);
        if (i != 1) {
            return R.fail("删除失败");
        }
        return R.success();
    }

    /**
     * wx
     * 条件查询
     * 查询套餐
     */
    @SaIgnore
    @PostMapping("/wx")
    public R<Page<PackageWxVo>> conditionPackageList(@RequestBody PackageQuery packageQuery) {
        Page<PackageWxVo> packageWxVo = packageService.getPackageWxVo(packageQuery.getPageNum(), packageQuery
            .getPageSize(), packageQuery.getName(), packageQuery.getLable(), packageQuery.getTypeId());
        return R.success(packageWxVo);
    }

    /**
     * wx
     * 根据id查询套餐详情
     */
    @SaIgnore
    @GetMapping("/wx/{id}")
    public R<PackageWxVo> getPackageWxDeById(@PathVariable("id") Integer id) {
        return R.success(packageService.getPackageWxDe(id));
    }

}
